Ext.namespace("myapp.widgets");

myapp.widgets.ProductsTree = function(myId, myLabels, myConfig) {
   this.id = myId;
   this.labels = myLabels;
   this.config = myConfig;
   this.tree = null;

   this.addEvents({
        "treeIsNotEmpty" : true
    });
}


Ext.extend(myapp.widgets.ProductsTree, Ext.util.Observable, {
    init : function() {
        var configTree = this.initConfigTree();
        this.initTree(configTree);
        this.tree.render("treeContainer");
        this.tree.expandAll();
    },

    initLoader : function() {
        var loader = new myapp.widgets.ProductsTreeLoader({
               labels: this.labels,
               id: this.id + "_treeLoader",
               dataSrc: this.config.dataSrc
        });

        loader.on("treeIsNotEmpty", function() {
            var result = this.treeIsNotEmptyHandler();
            this.fireEvent("treeIsNotEmpty", result);
        }, this);

        return loader;
    },

   initConfigTree : function() {
        return {
            rootVisible: false,
            labels: this.labels,
            height: 600,
            autoScroll: true,
            columns: [{
                    header: this.labels.summary,
                    width: 300,
                    dataIndex: 'label'
                },{
                    header: this.labels.unitPrice,
                    width: 100,
                    dataIndex: 'unitPrice',
                },{
                    header: this.labels.quantity,
                    width: 80,
                    dataIndex: 'quantity',
                }],
            loader: this.initLoader()
        };
    },

    initTree : function(configTree) {
        this.tree = new Ext.tree.TreePanel(configTree);

        var root = new Ext.tree.AsyncTreeNode({
            text: this.labels.products
        });

        root.on("load", function(node) {
            this.fireEvent("treeIsNotEmpty");
        }, this);

        this.tree.setRootNode(root);
    },

    treeIsNotEmptyHandler : function() {
         var result = 0;

         this.tree.cascade(function(node) {
            result += node.attributes.unitPrice * node.attributes.quantity;
         });

         return result;
    },
});


myapp.widgets.ProductsTreeLoader = function(myConfig) {
   myapp.widgets.ProductsTreeLoader.superclass.constructor.call(this, myConfig);

   this.id = myConfig.id;
   this.labels = myConfig.labels;
   this.dataSrc = myConfig.dataSrc;
}

Ext.extend(myapp.widgets.ProductsTreeLoader, Ext.tree.TreeLoader, {

   load : function(node, callback) {
      this.buildNodes(node);
   },

   buildNodes : function(parentNode) {
      var successFn = function(data) {
         var children = eval(data);

         for (var i = 0; i < children.length; i++) {
            var childNode = new Ext.tree.TreeNode({
               text: children[i].label,
               label: children[i].label,
               unitPrice: children[i].unitPrice,
               quantitiy: children[i].quantity
            });

            parentNode.appendChild(childNode);
         }
      }

      var failureFn = function() {
      }

      Ext.Ajax.request({
         url: this.config.dataSrc,
         success: successFn,
         failure: failureFn,
         params: { parent: parentNode.attributes.label }
      });
   }
});


